$(function() {


    let load = axios.create({
        baseURL: "http://localhost:3000/static/data/"

    })

    function loadListData() {
        let options = {
            url: "/ind.json"
        }
        load(options)
            .then((res) => {
                var re = location.hash
                console.log(re);
                var a = re.match(/a(\d?)/)[1];
                var b = re.match(/b(\d+)/)[1];
                var data = res.data
                var goods = data[a].data[b]
                console.log(goods);
                $(".img-box img").attr(`src`, `${goods.pic}`)
                $(".big-img img").attr("src", `${goods.pic}`)
                $(".dright>.sp1 ").text(`${goods.name}`)
                $(".dright>h5>.sp2").text(`${goods.name}`)
                $(".dcontent>.p1>.sp2").text(`￥${goods.crossedPrice}`)
                var sum = 1
                $(".cost>input").val(`${sum}`)
                    // 获取localStorage数据
                var carda = []
                var da = {
                    id: "",
                    sum: ""
                }
                carda.push(da)

                $(".cost>.sp2").on("click", function() {
                    sum += 1
                    $(".cost>input").val(`${sum}`)


                })
                $(".cost>.sp1").on("click", function() {
                    if (sum <= 0) {
                        sum = 0
                        $(".cost>input").val(`${sum}`)

                    } else {
                        sum -= 1
                        $(".cost>input").val(`${sum}`)

                    }

                })
                var addsum = document.querySelector(".cost>input")




                // da.sum = $(".cost>input").val()

                // 加入购物车
                var TOKEN = cookie("TOKEN")

                $(".settle>.sp1").on("click", function() {
                    if (TOKEN.length > 0) {
                        let s = carda.length - 1

                        carda[s].id = re
                        carda[s].sum = $(".cost>input").val()
                            // 储存到local storage
                            // localStorage.setItem("cart", JSON.stringify(this.cart_data))
                        localStorage.setItem("cart", JSON.stringify(carda))
                            // location.href = "./shopcar.html"
                    } else {
                        location.href = "./login.html"
                    }
                })


                $(".detial").append(`${goods.productChannelVoList[0].detailHtml}`)

                $(".img-box").on("mouseover", function() {
                    $(".focus").removeClass("hide")
                    $(".big-img").removeClass("hide")
                })
                $(".img-box").on("mouseout", function() {
                    $(".focus").addClass("hide")
                    $(".big-img").addClass("hide")
                })
                $(".gocar").on("click", function() {


                    // 储存
                    // 有没有购物车数据
                    // 有，我们直接加数据，没有创建购物车+数据











                })
                new Magnifier({
                    el: ".goods-detail",
                    img: ".img-box",
                    focus: ".focus",
                    big: ".big-img",
                    big_img: ".big-img img"
                });

                // function addCart(id, data) {
                //     // 判断当前的id是否存在于购物车列表之中
                //     if (id in this.cart_data) {
                //         // 如果存在就让商品数量自增; 
                //         this.cart_data[id].count++;
                //     } else {
                //         // 如果不存在就放入一个初始数据; 
                //         data.count = 1;
                //         this.cart_data[id] = data;
                //     }
                //     this.save();
                // }

            });


    }

    loadListData();

})